'use strict';

const Controller = require('egg').Controller;
class CouponsController extends Controller {
    // 创建 修改 券
    async edit() {
        const { ctx, app } = this
        ctx.validate({
            id: {
                type: 'int',
                required: false,
                desc: 'id'
            },
            shop_id: {
                type: 'string',
                required: true,
                desc: '商家id'
            },
            title: {
                type: 'string',
                required: true,
                desc: '券标题'
            },
            detail: {
                type: 'string',
                required: true,
                desc: '券简介'
            },
            pay_price: {
                type: 'int',
                required: true,
                desc: '支付金额'
            },
            market_price: {
                type: 'int',
                required: true,
                desc: '市场价格'
            },
            coupon_limit: {
                type: 'string',
                required: true,
                range: {
                    in: [0, 1]
                },
                defValue: 0,
                desc: '领券限制'
            },
            total_number: {
                type: 'int',
                required: true,
                desc: '总数量'
            },
            surplus_number: {
                type: 'int',
                required: true,
                desc: '剩余数量'
            },
            write_number: {
                type: 'int',
                required: true,
                desc: '核销数量'
            },
            statr_time: {
                type: 'string',
                required: true,
                desc: '激活劵_开始时间'
            },
            end_time: {
                type: 'string',
                required: true,
                desc: '激活劵_结束时间'
            },
            fixed_days: {
                type: 'int',
                desc: '使用有效期_激活后好多天有效'
            },
            fixed_statr_time: {
                type: 'string',
                desc: '使用有效期_开始时间'
            },
            fixed_end_time: {
                type: 'string',
                desc: '使用有效期_结束时间'
            },
            use_otice: {
                type: 'string',
                required: true,
                desc: '使用须知'
            },
            goods_detaill: {
                type: 'string',
                required: true,
                desc: '商品详情',
            },
            refund_fee1: {
                type: 'int',
                required: true,
                desc: '退款手续费1',
            },
            refund_fee2: {
                type: 'int',
                required: true,
                desc: '退款手续费2',
            },
            refund_fee3: {
                type: 'int',
                required: true,
                desc: '退款手续费3',
            },
            service_charge: {
                type: 'int',
                required: true,
                desc: '商家手续费'
            },
            agent_purchase: {
                type: 'string',
                required: true,
                range: {
                    in: [0, 1]
                },
                desc: '代理购买 0禁用 1启用'
            },
            agent_price: {
                type: 'string',
                desc: '代理购买金额 '
            },
            agent_number: {
                type: 'int',
                desc: '最低购买数量'
            },
            agent_otice: {
                type: 'string',
                desc: '代理须知'
            },
            agent_charge: {
                type: 'int',
                desc: '代理提成比例'
            },
            sort: {
                type: 'int',
                required: true,
                desc: '排序'
            },
            status: {
                type: 'string',
                required: true,
                range: {
                    in: [0, 1]
                },
                defValue: 1,
                desc: '状态'
            }
        })
        const { id, shop_id, title, detail, pay_price, market_price, coupon_limit, total_number, surplus_number, write_number, statr_time, end_time, fixed_days, fixed_statr_time, fixed_end_time, use_otice, goods_detaill, refund_fee1, refund_fee2, refund_fee3, service_charge, agent_purchase, agent_price, agent_number, agent_otice, agent_charge, sort, status } = ctx.request.body
        // 验证 商户id 
        let shang = await app.model.Shop.findByPk(shop_id)
        if (!shang) {
            ctx.throw(400, '门店不存在')
        }
        //验证 使用期
        if (fixed_days && (fixed_statr_time || fixed_end_time)) {
            ctx.throw(400, '使用期只能选择一种')
        }
        // 验证 代理
        if (agent_purchase == '1') {
            console.log('1111111111111')
            if (!agent_price || !agent_number || !agent_otice || !agent_charge) {
                ctx.throw(400, '代理开启时,代理购买金额.最低购买数量.代理须知.代理提成比例 为必填')
            }
        }
        let listOne, listTwo
        if (id == 0) {//创建
            listOne = await app.model.Coupons.create({
                shop_id, title, detail, pay_price, market_price, coupon_limit, total_number, surplus_number: total_number, write_number, statr_time, end_time, fixed_days, fixed_statr_time, fixed_end_time, use_otice, goods_detaill, refund_fee1, refund_fee2, refund_fee3, service_charge, agent_purchase, agent_price, agent_number, agent_otice, agent_charge, sort, status
            })
        } else { //修改
            listTwo = await app.model.Coupons.findOne({
                where: {
                    id: id
                }
            })
            if (!listTwo) {
                return ctx.apiFail('数据不存在')
            }
            // console.log('-----------',listTwo)
            listOne = await listTwo.update({
                shop_id, title, detail, pay_price, market_price, coupon_limit, total_number, surplus_number, write_number, statr_time, end_time, fixed_days, fixed_statr_time, fixed_end_time, use_otice, goods_detaill, refund_fee1, refund_fee2, refund_fee3, service_charge, agent_purchase, agent_price, agent_number, agent_otice, agent_charge, sort, status
            })
        }
        ctx.apiSuccess(listOne)
    }
    // 券列表
    async list() {
        const { ctx, app } = this
        const { shop_id, title } = ctx.query
        let where = {}, Op = app.Sequelize.Op;
        if (shop_id != '' && shop_id > 0) {
            where.shop_id = shop_id
        }
        if (title != '' && title != undefined) {
            where.title = {
                [Op.like]: '%' + title + '%'
            }
        }
        let list = await ctx.page(app.model.Coupons, where)
        ctx.apiSuccess(list)
    }
    //删除
    async del() {
        const { ctx, app } = this
        const id = ctx.params.id
        let list = await app.model.Coupons.findByPk(id)
        if (!list) {
            ctx.throw(400, '数据不存在')
        }
        list.destroy()
        ctx.apiSuccess('删除成功')
    }
    //领取 列表
    async collarlist() {
        const { ctx, app } = this
        const { nickname, source, status, coupons_id } = ctx.query
        ctx.validate({
            coupons_id: {
                type: 'int',
                required: true,
                desc: '券id'
            },
            source: {
                type: 'string',
                required: false,
                range: {
                    in: ['1', '2']
                },
                desc: '券来源'
            }
        })
        let where = {}, Op = app.Sequelize.Op
        if(source!=''&&source!=undefined){//来源
            where.source=source
        }
        if(status!=''&&status!=undefined){//券 状态
            where.status=status
        }
        let listOne=[],listTwo
        if(nickname != '' && nickname != undefined){
           let list=await app.model.WxUser.findAll({
                where:{
                    nickname: {
                        [Op.like]: `%${nickname}%`
                    }
                },
                attributes: ['id']
            })
            listOne=list.map(it=>it.id)
        }
        //微信 昵称
        let where1=listOne.length?{id:listOne}:{}
        listTwo=await ctx.page(app.model.CouponsCollar,where,{
            include: [{
                model: app.model.WxUser,
                where:where1,
                attributes: ['id','nickname']
            }]
        })
        ctx.apiSuccess(listTwo)
    }
    //代理 领券 列表
    async agentlist(){
        const {ctx,app}=this
        const { nickname, coupons_id } = ctx.query
        ctx.validate({
            coupons_id: {
                type: 'int',
                required: true,
                desc: '券id'
            }
        })
        let where = {}, Op = app.Sequelize.Op
        let listOne=[],listTwo
        if(nickname != '' && nickname != undefined){
           let list=await app.model.WxUser.findAll({
                where:{
                    nickname: {
                        [Op.like]: `%${nickname}%`
                    }
                },
                attributes: ['id']
            })
            listOne=list.map(it=>it.id)
        }
        //微信 昵称
        let where1=listOne.length?{id:listOne}:{}
        listTwo=await ctx.page(app.model.CouponsAgent,where,{
            include: [{
                model: app.model.WxUser,
                where:where1,
                attributes: ['id','nickname']
            }]
        })
        ctx.apiSuccess(listTwo)
    }

    
    
}

module.exports = CouponsController;
